package com.origin.baselibrary.activity.style

import android.graphics.Color
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.origin.baselibrary.R
import com.origin.baselibrary.bean.TabEntityBean
import com.origin.baselibrary.databinding.LayoutActivityViewpagerBinding
import kotlinx.android.synthetic.main.layout_activity_viewpager.*

abstract class ViewPagerActivity(override var isSafeDis: Boolean = true) : ImmersiveActivity<LayoutActivityViewpagerBinding>(R.layout.layout_activity_viewpager, isSafeDis = isSafeDis) {

    private val mTitles = mutableListOf<String>()
    private val mIconUnselectIds = mutableListOf<Int>()
    private val mIconSelectIds = mutableListOf<Int>()
    private val mFragments = mutableListOf<Fragment>()

    private val mTabEntities = ArrayList<CustomTabEntity>()

    private var textSelectColor = Color.parseColor("#fff000")
    private var textUnselectColor = Color.parseColor("#ff0000")

    fun getRlContent(): RelativeLayout {

        return contentViewBinding.rlContent
    }


//    override fun contentView(): Int = R.layout.layout_activity_viewpager

    fun setViewpagerScroll(isScroll: Boolean) {
        contentViewBinding.baseViewpager.setScroll(isScroll)
    }

    override fun initContentView() {
        if (!checkPageReasonable()) {
            throw Exception("滑动页面数据异常")
        }
        textSelectColor = getTextSelectColor()
        textUnselectColor = getTextUnSelectColor()
        pageDraw()
    }

    open fun getTextSelectColor(): Int {
        return Color.parseColor("#fff000")
    }

    open fun getTextUnSelectColor(): Int {
        return Color.parseColor("#000000")
    }


    private fun pageDraw() {
        contentViewBinding.tlBase.textSelectColor = textSelectColor
        contentViewBinding.tlBase.textUnselectColor = textUnselectColor
        if (isShowViewPagerController()) {
            for ((i, v) in mTitles.withIndex()) {
                mTabEntities.add(TabEntityBean(v, mIconSelectIds[i], mIconUnselectIds[i]))
            }
            contentViewBinding.tlBase.setTabData(mTabEntities)
        }


        baseViewpager.adapter = MyPagerAdapter(supportFragmentManager)
        baseViewpager.offscreenPageLimit = mTitles.size
        baseViewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

            }

            override fun onPageSelected(p0: Int) {
                if (isShowViewPagerController()) {
                    tlBase.currentTab = p0
                }

            }

        })
        if (isShowViewPagerController()) {
            tlBase.setOnTabSelectListener(object : OnTabSelectListener {
                override fun onTabSelect(position: Int) {
                    if (toFragment(position)) {
                        tlBase.currentTab = preTabIndex
                        return
                    }
                    preTabIndex = position
                    baseViewpager.currentItem = position
                }

                override fun onTabReselect(position: Int) {
                }

            })
        }


    }


    fun setViewPagerIndex(num: Int) {
        baseViewpager.currentItem = num;
    }

    private var preTabIndex = 0

    private inner class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return mTitles[position]
        }

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }
    }

    open fun toFragment(index: Int): Boolean {
        return false
    }

    // 是否需要控制
    open fun isShowViewPagerController(): Boolean {
        return true;
    }

    // return false 页面不合理
    private fun checkPageReasonable(): Boolean {
        setSelectorUi(mIconUnselectIds, mIconSelectIds, mTitles, mFragments)
        if (!isShowViewPagerController()) {
            val params = tlBase.layoutParams as (LinearLayout.LayoutParams)
            params.height = 0;
            tlBase.layoutParams = params;
            return true
        }


        if (mIconUnselectIds.size == 0 || mIconSelectIds.size == 0
            || mFragments.size == 0 || mTitles.size == 0
        ) {
            return false
        }

        if (mIconUnselectIds.size == mIconSelectIds.size && mIconUnselectIds.size == mFragments.size
            && mTitles.size == mFragments.size
        ) {
            return true
        }

        return false

    }


    abstract fun setSelectorUi(
        iconUnselectIds: MutableList<Int>,
        iconSelectIds: MutableList<Int>,
        titles: MutableList<String>,
        fragments: MutableList<Fragment>
    )
}